import Cookies from "js-cookie"
export default{
    namespaced:true,
    actions:{
    
    },
    //准备mutations-用于操作数据（state）
    mutations:{
        selectMenu(state,val){
           //console.log(state,val)
            if(val.name !=="home"){
                const index = state.tabList.findIndex(item => item.name===val.name)
                if(index===-1){
                    state.tabList.push(val)
                }
            }
        },
        closeTag(state,item){
           const index = state.tabList.findIndex(val=>val.name===item.name)
           state.tabList.splice(index,1)
        },
        setMenu(state,val){
          state.menu=val
          Cookies.set("menu",JSON.stringify(val))

        },
        //动态路由
        addMenu(state,router){
            if(!Cookies.get('menu')) return
            const menu=JSON.parse(Cookies.get('menu'))

            state.menuData=menu
            //组装动态路由
            const menuAarry=[]
            menu.forEach(item => {
                if(item.children){
                    item.children =  item.children.map(item=>{
                        item.component=()=>import(`../views/${item.url}`)
                        return item
                    })
                menuAarry.push(...item.children)
                }
                else{
                   item.component=()=>import(`../views/${item.url}`)
                    menuAarry.push(item)
                }
            })
            console.log(menuAarry)


            menuAarry.forEach(item=>{

                router.addRoute('Main',item)
               
            })
            
            
        }
    },
    state:{
        tabList:[
            {
                path: "/",
                name: "home",
                label: "首页",
                icon: "s-home",
                url: "Home/Home",
              },
        ],
        menuData:[]
     },
     //准备getters-用于将state中的数据进行加工
     getters:{
     
    }
}